package com.itheima.advantagesup.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.advantagesup.dto.UserDTO;
import com.itheima.advantagesup.entity.SysUser;
import com.itheima.advantagesup.mapper.SysUserMapper;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.vo.PageUserVO;
import com.itheima.advantagesup.vo.RoleInfoVO;
import com.itheima.advantagesup.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * SysUserServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/26 14:58
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    private final SysUserMapper sysUserMapper;

    @Override
    public PageUserVO page(String login_name, Integer status, String phone, Integer page, Integer limit) {
        List<SysUser> sysUserList = sysUserMapper.selectByLoginNameAndStatusAndPhone(login_name,status,phone);
        PageHelper.startPage(page,limit);
        PageInfo pageInfo = new PageInfo(sysUserList);
        //PageInfo<SysUser> pageInfo = new PageInfo<>(sysUserList);
        List<SysUser> sysUserList2 = pageInfo.getList();

        List<UserInfoVO> collect = sysUserList2.stream().map(new Function<SysUser, UserInfoVO>() {
            @Override
            public UserInfoVO apply(SysUser sysUser) {
                UserInfoVO userInfoVO = sysUser.transformUserInfoVO();
                //List<String> role_id = userInfoVO.getRole_id();
                /*List<String> roleNames = sysUserMapper.selectByUserId(userInfoVO.getRole_id());//userInfoVO.getRole_id()是个集合
                userInfoVO.setRolename(roleNames);*/
                return userInfoVO;
            }
        }).collect(Collectors.toList());
        for (UserInfoVO userInfoVO : collect) {
            List<String> roleNames = sysUserMapper.selectByUserId(userInfoVO.getRole_id());//userInfoVO.getRole_id()是个集合
            userInfoVO.setRolename(roleNames);
        }


        PageUserVO pageUserVO = new PageUserVO();
        pageUserVO.setCount(pageInfo.getTotal());
        pageUserVO.setTotalPages(pageInfo.getPages());
        pageUserVO.setPageSize(limit);
        pageUserVO.setCurrentPage(pageInfo.getPageNum());
        pageUserVO.setData(collect);
        return pageUserVO;
    }

    /**
     * 添加用户
     * @param userDTO
     */
    @Override
    public void add(UserDTO userDTO) {
        SysUser sysUser = userDTO.transformSysUser();
        String id = UUID.randomUUID().toString();
        sysUser.setId(id);
        sysUser.setCreateDate(LocalDateTime.now());
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUserMapper.add(sysUser);
    }

    /**
     * 回显
     * @param id
     * @return {@link RoleInfoVO}
     */
    @Override
    public UserInfoVO rowInfo(String id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        UserInfoVO userInfoVO = sysUser.transformUserInfoVO();
        return userInfoVO;
    }

    /**
     * 修改用户
     * @param userDTO
     */
    @Override
    public void update(UserDTO userDTO) {
        SysUser sysUser = userDTO.transformSysUser();
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUserMapper.updateSysUserById(sysUser);
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void delete(String id) {
        sysUserMapper.deleteById(id);
    }
}
